package huge

import (
	"qm_system_server/server/global"
	"qm_system_server/server/model/huge"
    hugeReq "qm_system_server/server/model/huge/request"
)

type QmAccessTokensService struct {
}

// CreateQmAccessTokens 创建qmAccessTokens表记录
// Author [piexlmax](https://github.com/piexlmax)
func (qmAccessTokensService *QmAccessTokensService) CreateQmAccessTokens(qmAccessTokens *huge.QmAccessTokens) (err error) {
	err = global.GVA_DB.Create(qmAccessTokens).Error
	return err
}

// DeleteQmAccessTokens 删除qmAccessTokens表记录
// Author [piexlmax](https://github.com/piexlmax)
func (qmAccessTokensService *QmAccessTokensService)DeleteQmAccessTokens(ID string) (err error) {
	err = global.GVA_DB.Delete(&huge.QmAccessTokens{},"id = ?",ID).Error
	return err
}

// DeleteQmAccessTokensByIds 批量删除qmAccessTokens表记录
// Author [piexlmax](https://github.com/piexlmax)
func (qmAccessTokensService *QmAccessTokensService)DeleteQmAccessTokensByIds(IDs []string) (err error) {
	err = global.GVA_DB.Delete(&[]huge.QmAccessTokens{},"id in ?",IDs).Error
	return err
}

// UpdateQmAccessTokens 更新qmAccessTokens表记录
// Author [piexlmax](https://github.com/piexlmax)
func (qmAccessTokensService *QmAccessTokensService)UpdateQmAccessTokens(qmAccessTokens huge.QmAccessTokens) (err error) {
	err = global.GVA_DB.Save(&qmAccessTokens).Error
	return err
}

// GetQmAccessTokens 根据ID获取qmAccessTokens表记录
// Author [piexlmax](https://github.com/piexlmax)
func (qmAccessTokensService *QmAccessTokensService)GetQmAccessTokens(ID string) (qmAccessTokens huge.QmAccessTokens, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&qmAccessTokens).Error
	return
}

// GetQmAccessTokensInfoList 分页获取qmAccessTokens表记录
// Author [piexlmax](https://github.com/piexlmax)
func (qmAccessTokensService *QmAccessTokensService)GetQmAccessTokensInfoList(info hugeReq.QmAccessTokensSearch) (list []huge.QmAccessTokens, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&huge.QmAccessTokens{})
    var qmAccessTokenss []huge.QmAccessTokens
    // 如果有条件搜索 下方会自动创建搜索语句
    if info.StartCreatedAt !=nil && info.EndCreatedAt !=nil {
     db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
    }
	err = db.Count(&total).Error
	if err!=nil {
    	return
    }

	if limit != 0 {
       db = db.Limit(limit).Offset(offset)
    }

	err = db.Find(&qmAccessTokenss).Error
	return  qmAccessTokenss, total, err
}
